package com.aust.furniture.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.aust.furniture.common.ResultMapUtil;
import com.aust.furniture.entity.Customer;
import com.aust.furniture.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 顾客信息相关的controller
 */
@Controller
@RequestMapping(value = "/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    /**
     * 转向顾客信息页面
     */
    @RequestMapping
    public String customer(){
        return "/customer";
    }

    /**
     * 分页查询顾客信息列表
     */
    @RequestMapping(value = "/customerQueryPage")
    @ResponseBody
    public Object customerQueryPage(String param, @RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int limit){
        try{
            IPage<Customer> iPage = customerService.selectCustomerPage(page,limit,param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向顾客信息新增页面
     */
    @RequestMapping(value = "/customerPage")
    public String customerPage(){
        return "/customerPage";
    }

    /**
     * 添加一个顾客信息
     */
    @RequestMapping(value = "/customerAdd")
    @ResponseBody
    public Object customerAdd(Customer customer){
        try{
            int i = customerService.addCustomer(customer);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向顾客信息编辑页面
     */
    @RequestMapping(value = "/customerQueryById")
    public String customerQueryById(@RequestParam(name = "id",required = true)Integer id, Model model){
        Customer customer = customerService.queryCustomerById(id);
        model.addAttribute("obj",customer);
        return "/customerPage";
    }

    /**
     * 修改一个顾客信息
     */
    @RequestMapping(value = "/customerEdit")
    @ResponseBody
    public Object customerEdit(Customer customer){
        try{
            int i = customerService.editCustomer(customer);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个顾客信息
     */
    @RequestMapping(value = "/customerDelById")
    @ResponseBody
    public Object customerDelById(Integer id){
        try{
            int i = customerService.delCustomerById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }


}





















